package com.chuanjiabao.wisdom.adapter;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.databinding.DataBindingUtil;
import android.os.Build;
import android.support.annotation.RequiresApi;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.TextView;

import com.chuanjiabao.wisdom.R;
import com.chuanjiabao.wisdom.bean.FamilyResCallbackBean;
import com.chuanjiabao.wisdom.databinding.ItemMonthBinding;
import com.chuanjiabao.wisdom.listener.OnItemClickListener;
import com.chuanjiabao.wisdom.listener.OnThirdClickListener;
import com.chuanjiabao.wisdom.ui.activity.FindToSeeFamilyActivity;
import com.chuanjiabao.wisdom.ui.view.TaoLinear;
import com.chuanjiabao.wisdom.util.MultiGapDecoration;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class MonthAdapter extends RecyclerView.Adapter<BaseRecyclerViewHolder> {
    private List<String> strings;
    private Map<String, List<FamilyResCallbackBean.DataBeanX.DataBean>> mapInfos;
    private Context mcontext;
    boolean isShow, isSecret;
    FamilyResAdapter familyResAdapter;
    public List<CheckBox> listCheckBoxs = new ArrayList<>();
    OnThirdClickListener onThirdClickListener;
    public List<FamilyResAdapter> listAdapters = new ArrayList<>();
    private MultiGapDecoration decoration = new MultiGapDecoration(2);

    public void setOnThirdClickListener(OnThirdClickListener onThirdClickListener) {
        this.onThirdClickListener = onThirdClickListener;
    }

    public MonthAdapter(Context context, List<String> strings,
                        Map<String, List<FamilyResCallbackBean.DataBeanX.DataBean>> mapInfos,
                        boolean isShow, boolean isSecret) {
        this.mcontext = context;
        this.strings = strings;
        this.isShow = isShow;
        this.mapInfos = mapInfos;
        this.isSecret = isSecret;
    }

    @Override
    public BaseRecyclerViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        ItemMonthBinding binding = DataBindingUtil.inflate(LayoutInflater.from(mcontext),
                R.layout.item_month, parent, false);
        binding.setAdapter(this);
        return new TourViewHolder(binding);
    }

    @SuppressLint("SetTextI18n")
    @Override
    public void onBindViewHolder(final BaseRecyclerViewHolder holder, final int position) {
        holder.bindData(strings.get(position));
        final TourViewHolder tv = (TourViewHolder) holder;
        tv.tvModuleMonth.setText(strings.get(position));
        familyResAdapter = new FamilyResAdapter(mcontext, mapInfos.get(strings.get(position)), isShow);
        TaoLinear taoLinear = new TaoLinear(mcontext, 3);
        taoLinear.setScrollEnabled(true);
        tv.recycler.setLayoutManager(taoLinear);
        tv.recycler.setItemAnimator(new DefaultItemAnimator());
        tv.recycler.removeItemDecoration(decoration);
        tv.recycler.addItemDecoration(decoration);
        tv.recycler.setAdapter(familyResAdapter);
        familyResAdapter.setOnItemClickLitener(new OnItemClickListener() {
            @RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN)
            @Override
            public void onItemClick(View view, int pos) {
                int currentIndex = 0;
                ArrayList<FamilyResCallbackBean.DataBeanX.DataBean> items = new ArrayList<>();
                for (int i = 0; i < strings.size(); i++) {
                    if (i == position) currentIndex = items.size() + pos;
                    items.addAll(mapInfos.get(strings.get(i)));
                }

                FindToSeeFamilyActivity.setData(items);

                Intent intent = new Intent(mcontext, FindToSeeFamilyActivity.class);
                intent.putExtra("module_position", currentIndex);
                intent.putExtra("module_isSecret", isSecret);
                mcontext.startActivity(intent);
            }
        });
        listAdapters.add(familyResAdapter);

        familyResAdapter.setOnSecondClickListener((pos, type, url) -> {
            if (onThirdClickListener != null) {
                onThirdClickListener.setOnThirdClickListener(position, pos, type, url);
            }
        });

        listCheckBoxs.addAll(familyResAdapter.checkBoxes);
    }

    /**
     * 改变子分类checkbox显示或者隐藏
     */
    public void setCheckBoxStatus(boolean isShow, int position) {
        this.isShow = isShow;
        if (isShow) {
            familyResAdapter.setCheckTextSelectAllYes();
        } else {
            familyResAdapter.setCheckTextSelectAllNo();
        }
        notifyItemChanged(position);
    }


    @Override
    public int getItemCount() {
        return strings.size();
    }

    public class TourViewHolder extends BaseRecyclerViewHolder<String> {
        ItemMonthBinding functionBinding;
        TextView tvModuleMonth;
        RecyclerView recycler;

        public TourViewHolder(ItemMonthBinding dataBinding) {
            super(dataBinding.getRoot());
            functionBinding = dataBinding;
            tvModuleMonth = dataBinding.tvModuleMonth;
            recycler = dataBinding.recycler;
        }

        @Override
        public void bindData(String data) {
            functionBinding.setBean(data);
        }
    }

    @Override
    public int getItemViewType(int position) {
        return strings.size();
    }

    public void removePosition(int position) {
        familyResAdapter.setChangeItem(position);
        notifyDataSetChanged();
    }

}